package com.redsoil.house.ui.login.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.redsoil.house.R;
import com.redsoil.house.base.BaseActivity;
import com.redsoil.house.model.bean.UserVO;
import com.redsoil.house.ui.login.contract.LoginContract;
import com.redsoil.house.ui.login.presenter.LoginPresenter;
import com.redsoil.house.ui.main.activity.MainActivity;
import com.redsoil.house.utils.EnglishUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ntalker.api.Ntalker;
import cn.ntalker.manager.inf.outer.NtalkerCoreCallback;

/**
 * 登陆页面
 * dengyanxia   chenpeishan
 * jollyznl
 * 123456
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View{

    @BindView(R.id.warn_tv)
    TextView warn_tv;

    @BindView(R.id.username_edit)
    EditText username_edit;

    @BindView(R.id.password_edit)
    EditText password_edit;

    @BindView(R.id.fotget_pwd_tv)
    TextView fotget_pwd_tv;

    @BindView(R.id.login_button)
    Button login_button;

    @BindView(R.id.tos_tv)
    TextView tos_tv;

    @BindView(R.id.no_tos_tv)
    TextView no_tos_tv;

    @BindView(R.id.checkbox)
    CheckBox checkbox;

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initEventAndData() {
        tos_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tos_tv.getPaint().setAntiAlias(true);//抗锯齿

        no_tos_tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        no_tos_tv.getPaint().setAntiAlias(true);//抗锯齿
    }

    @OnClick(R.id.login_button)
    void onLoginButton(){
        login_button.setEnabled(false);
        warn_tv.setVisibility(View.INVISIBLE);

        //用户登陆
        String username = username_edit.getText().toString();
        String userpassword = password_edit.getText().toString();

//            //检测密码
//        if(!EnglishUtil.CheckPassword(userpassword))
//        {
//            ToastUtils.showLongToast(getString(R.string.register_password_require));
//            login_button.setEnabled(true);
//            return;
//        }


        //1.判断账号不能为空
        if (StringUtils.isSpace(username) || StringUtils.isSpace(userpassword)) {
            ToastUtils.showShortToast("用户名或者密码不能为空");
            login_button.setEnabled(true);
        } else {
//            mPresenter.login("zhangsan","123456");

            if(checkbox.isChecked()){
                mPresenter.login(username, userpassword);
            }else {
                ToastUtils.showShortToast("请阅读并同意用户使用条款");
                login_button.setEnabled(true);
            }

        }

    }

    @OnClick(R.id.tos_tv)
    void onClickTosTv(){
        startActivity(LawActivity.newInstance(this,true));
    }

    @OnClick(R.id.no_tos_tv)
    void onClickNoTosTv(){
        startActivity(LawActivity.newInstance(this,false));
    }

    @Override
    public void loginSuccess(UserVO userData) {
        login_button.setEnabled(true);

        Ntalker.getInstance().login(userData.getUsername(), userData.getName(),null);
//        Ntalker.getInstance().login("wdq", "weidingqiang",null);
        startActivity(MainActivity.newInstance(getApplicationContext()));
    }

    @Override
    public void responeError(String errorMsg) {
        login_button.setEnabled(true);
        warn_tv.setText(errorMsg);
        warn_tv.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.fotget_pwd_tv)
    void onFotgetPwd(){
        startActivity(ForgotPwdActivity.newInstance(this));
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        return intent;
    }
}
